package com.controller;/**
 * @program: SmbmsDemo
 * @description: 用户登录控制器
 * @author: Mr.Kang
 * @create: 2020-05-30 19:23
 **/

import com.pojo.User;
import com.service.UserService;
import com.service.UserServiceImpl;
import com.tools.Constants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @author 康康真滴暖
 * @date 2020-05-30 19:23
 */

@Controller
public class LoginController {
    private Logger log= LogManager.getLogger(LoginController.class);

    @Resource
    private UserService userService;

    /**
     * 用户登录方法
     * @return
     */
    @RequestMapping(value = "/login.do")
    public String UserLogin(HttpServletRequest request, Model model){
        log.info("login ============ " );
        //获取用户名和密码
        String userCode = request.getParameter("userCode");
        String userPassword = request.getParameter("userPassword");
        //调用service方法，进行用户匹配
        /*   UserService userService = new UserServiceImpl();*/
        User user = userService.login(userCode,userPassword);
        if(null != user){//登录成功
            //放入session
            request.getSession().setAttribute(Constants.USER_SESSION, user);
            //页面跳转（frame.jsp）
            return "frame";
        }else{
            //页面跳转（login.jsp）带出提示信息--转发
            request.setAttribute("error", "用户名或密码不正确");
            return "login";
        }
    }
    /**
     * 用户注销方法
     */
    @RequestMapping(value = "/jsp/logout.do")
    public String logout(HttpServletRequest request){
        //清楚session中的用户信息
        request.getSession().removeAttribute(Constants.USER_SESSION);
        //跳转到登录界面
        return "login";
    }
}
